<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: String.php
 * +----------------------------------------------------------------------
 * | Author: sam
 * +----------------------------------------------------------------------
 * | Description: 用于详细说明此程序文件完成的主要功能，与其他模块或函数的接口、输出值、取值范围、含义及参数间的控制、顺序或依赖等关系。
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
 * +----------------------------------------------------------------------
 * | 1.Date: 2018/12/28 Author: sam Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Library\String;


use Carbon\Carbon;
use Illuminate\Support\Str;

class Strings
{
    /**
     * Description:生成随机字符串
     * Author: Sam
     * DateTime: 2018/12/28 17:57
     * @param int $len "长度"
     * @param bool $str "是否要字符 true 有字符 false 纯数字"
     * @return string
     */
    public static function randomCode($len = 6, $str = false)
    {
        $string = 'asdfghjklqwerty456uzxcvbnm123789ASDFGHJLKQWERTYIPZXCVBNM';
        $str_len = strlen($string) - 1;
        $code = '';
        for ($i = 0; $i < $len; $i++) {
            if (!$str) {
                $code .= rand(0, 9);
            } else {
                $code .= $string[rand(0, $str_len)];
            }
        }
        return $code;
    }

    /**
     * Description:正则匹配省市区
     * Author: Sam
     * DateTime: 2019/1/3 13:54
     * @param $address
     * @return array
     */
    public static function getAddressCut($address)
    {
        $area = '';
        $province = '';
        preg_match('/(.*?(省|自治区|北京市|天津市|上海市|重庆市))/', $address, $matches);
        if (count($matches) > 1) {
            $province = $matches[count($matches) - 2];
            $address = str_replace($province, '', $address);
        }
        preg_match('/(.*?(市|自治州|地区|区划|县))/', $address, $matches);
        if (count($matches) > 1) {
            $city = $matches[count($matches) - 2];
            $address = str_replace($city, '', $address);
        }
        preg_match('/(.*?(区|县|镇|乡|街道))/', $address, $matches);
        if (count($matches) > 1) {
            $area = $matches[count($matches) - 2];
            $address = str_replace($area, '', $address);
        }
        $area_data = config('issue_gov_code');
        if (empty($province)) {
            if (!empty($city)) {
                foreach ($area_data as $k => $v) {
                    if ($v['city'] == $city) {
                        $province = $v['province'];
                        break;
                    }
                }
            } else {
                foreach ($area_data as $k => $v) {
                    if ($v['district'] == $area) {
                        $province = $v['province'];
                        $city = $v['city'];
                        break;
                    }
                }
            }
        }
        if (empty($city)) {
            if (in_array($province, ['北京市', '天津市', '上海市', '重庆市'])) {
                $city = $province;
            }
            foreach ($area_data as $k => $v) {
                if ($v['district'] == $area) {
                    $province = $v['province'];
                    $city = $v['city'];
                    break;
                }
            }
        }
        return [
            'province' => !empty($province) ? $province : '',
            'city' => !empty($city) ? $city : (in_array($province, ['北京市', '天津市', '上海市', '重庆市']) ? $province : ''),
            'area' => !empty($area) ? $area : '',
            'address' => $address,
        ];
    }

    /**
     * Description:生成唯一流水号
     * Author: Sam
     * DateTime: 2019/1/3 17:43
     * @param $user_id
     * @return string
     */
    public static function createSerialNumber($user_id)
    {
        $key = strtoupper(sha1($user_id) . Str::random(2));
        $len = strlen($key);
        $str = '';
        for ($i = 1; $i < 3; $i++) {
            $s = $key[$len - $i];
            $str .= ord($s);
        }
        return date('Ymd') . $str . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /**
     * Description: 生成系统loan_id
     * Author: Gxs
     * @param $id
     * @return string
     */
    public static function createSysLoanId($id)
    {
        $string = 'KD' . Carbon::now()->format('Ymd');
        $length = strlen($string) + strlen($id);
        $string .= self::generateRandomNumber(32 - $length) . $id;
        return $string;
    }

    /**
     * Description: 获取随机数字字符串
     * Author: Gxs
     * @param $length
     * @return string
     */
    public static function generateRandomNumber($length)
    {
        $chars = '0123456789';
        $string = '';
        for ( $i = 0; $i < $length; $i++ )
        {
            $string .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        return $string;
    }

    /**
     * 特殊字符
     * @param $str
     * @author luwei
     * @date 2019-03-07 17:35
     * @return string|string[]|null
     */
    public static function specialString($str){
        $pregs = '/select|insert|update|CR|document|LF|eval|delete|script|alert|\n|\'|\/\*|\#|\--|\ --|\/|\*|\-|\+|\=|\~|\*@|\*!|\$|\%|\^|\&|\(|\)|\/|\/\/|\.\.\/|\.\/|union|into|load_file|outfile/';
        $ret = preg_replace($pregs,"",$str);
        return $ret;
    }

    /**
     * Description: 分期计算
     * Author: Gxs
     * @param $amount
     * @param $period
     * @param $period_count
     * @return array average 每期, last 最后一期
     */
    public static function computeByPeriod($amount, $period, $period_count)
    {
        if (empty($amount)) {
            $average = 0;
            $last = 0;
        } else {
            $sub_period = bcsub($period_count, ($period - 1));
            $average = bcdiv($amount, $sub_period, 2);
            $last = bcsub($amount, bcmul($average, ($sub_period - 1), 2), 2);
        }

        return [
            'average' => $average,
            'last' => $last,
        ];
    }
}